﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class DSTREQYARN : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DSTREQYARNData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public DataRow dwVKPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdate = "", enddate = "";   
        #endregion
        public DSTREQYARN()
        {
            InitializeComponent();
            this.Name = "经纬纱流转卡";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DSTREQYARNData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ARTICLE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtARTICLE.Focus();
                /*
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct B.LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + "　A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI, A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS DIORDER "
                    + " FROM ABPOS A,LIEFDAT B,ABAUFM C "
                    + " WHERE A.ABPMANDANT = B.LSMANDANT AND A.ABPLIEFNR = B.LSLIEFNR "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAAUFLFD AND A.ABPAUFPOS = C.ABAAUFPOS "
                    + " AND A.ABPAUFART='PR'  AND A.ABPERLKZ <>'J' AND C.ABAAPOS='0001' "
                 ;
                */
                /*DataSet setResult = new DataSet();
                setResult = m_Data.LoadData(); 
                setUI.Merge(setResult);  
                this.DataContext = setUI;*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();            
                setUI.Clear();
                setUI.AcceptChanges();       
                string ARTICLE = txtARTICLE.Text.Trim();
                
                if (ARTICLE == "")
                {
                    ARTICLE = "AAA";
                }
                

                dwUI["ARTICLE"] = ARTICLE;                
                setResult = m_Data.BuildDSTREQYARNData(dwUI);
                setUI.Merge(setResult);
                MasterDg.DataContext = setUI.Tables["VKPOS"];
                //dlgWARPYARN.DataContext = setUI.Tables["WARPYARN"];
                //dlgWEFTYARN.DataContext = setUI.Tables["WEFTYARN"];
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }       
        private void btnWARP_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new DSTREQYARNData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            DSTREQYARNReport m_rpt = new DSTREQYARNReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        private void btnWEFT_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new DSTREQYARNData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            DSTREQYARNReport m_rpt = new DSTREQYARNReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcel("EXCEL");
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }
        private void MasterDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVKPOS = drv.Row;
                ReloadYARN(dwVKPOS);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
            
        }
        private void ReloadYARN(DataRow dwVKPOS)
        {
            if (setUI.Tables.Contains("WARPYARN"))
            {
                setUI.Tables["WARPYARN"].Clear();
                setUI.AcceptChanges();
            }
            if (setUI.Tables.Contains("WEFTYARN"))
            {
                setUI.Tables["WEFTYARN"].Clear();
                setUI.AcceptChanges();
            }            
            DataSet setResult = new DataSet();
            setResult = m_Data.GetWARPYARN(dwVKPOS);
            setUI.Merge(setResult);
            setResult = m_Data.GetWEFTYARN(dwVKPOS);
            setUI.Merge(setResult);
            dlgWARPYARN.DataContext = setUI.Tables["WARPYARN"];
            dlgWEFTYARN.DataContext = setUI.Tables["WEFTYARN"];
        }
    }
}
